package com.zixun.db;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.zixun.Models.DiaryInfo;
import com.zixun.Models.Message;
import com.zixun.Models.UserInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * 数据库操作
 */
public class DatabaseUtil {
    private MyHelper helper;

    public DatabaseUtil(Context context) {
        super();
        helper = new MyHelper(context);
    }

    //首页成长发展、婚恋问题、人际关系、心理障碍
    public boolean Insert5(Message message, String table) {
        SQLiteDatabase db = helper.getWritableDatabase();
        String sql = "insert into " + table
                + "(title,description) values ("
                + "'" + message.getTitle()
                + "' ," + "'" + message.getDescription() + "'" + ")";
        try {
            db.execSQL(sql);
            return true;
        } catch (SQLException e) {
            Log.e("err", "insert failed");
            return false;
        } finally {
            db.close();
        }
    }


    //获取首页数据,成长发展、婚恋问题、人际关系、心理障碍
    public List<Message> queryAll5(String table) {
        SQLiteDatabase db = helper.getReadableDatabase();
        List<Message> list = new ArrayList<Message>();
        Cursor cursor = db.query(table, null, null, null, null, null, null);

        while (cursor.moveToNext()) {
            Message person = new Message();
            person.setId(cursor.getInt(cursor.getColumnIndex("_id")));
            person.setTitle(cursor.getString(cursor.getColumnIndex("title")));
            person.setDescription(cursor.getString(cursor.getColumnIndex("description")));
            list.add(person);
        }
        db.close();
        return list;
    }


    //首页插入数据
    public boolean Insert(Message message) {
        SQLiteDatabase db = helper.getWritableDatabase();
        String sql = "insert into " + MyHelper.TABLE_NAME
                + "(title,description) values ("
                + "'" + message.getTitle()
                + "' ," + "'" + message.getDescription() + "'" + ")";
        try {
            db.execSQL(sql);
            return true;
        } catch (SQLException e) {
            Log.e("err", "insert failed");
            return false;
        } finally {
            db.close();
        }
    }

    //医生插入数据
    public boolean Insert1(Message message) {
        SQLiteDatabase db = helper.getWritableDatabase();
        String sql = "insert into " + MyHelper.DOCTOR_NAME
                + "(title,description) values ("
                + "'" + message.getTitle()
                + "' ," + "'" + message.getDescription() + "'" + ")";
        try {
            db.execSQL(sql);
            return true;
        } catch (SQLException e) {
            Log.e("err", "insert failed");
            return false;
        } finally {
            db.close();
        }

    }

    //获取首页数据
    public List<Message> queryAll() {
        SQLiteDatabase db = helper.getReadableDatabase();
        List<Message> list = new ArrayList<Message>();
        Cursor cursor = db.query(MyHelper.TABLE_NAME, null, null, null, null, null, null);

        while (cursor.moveToNext()) {
            Message person = new Message();
            person.setId(cursor.getInt(cursor.getColumnIndex("_id")));
            person.setTitle(cursor.getString(cursor.getColumnIndex("title")));
            person.setDescription(cursor.getString(cursor.getColumnIndex("description")));
            list.add(person);
        }
        db.close();
        return list;
    }

    //获取医生数据
    public List<Message> queryAll1() {
        SQLiteDatabase db = helper.getReadableDatabase();
        List<Message> list = new ArrayList<Message>();
        Cursor cursor = db.query(MyHelper.DOCTOR_NAME, null, null, null, null, null, null);

        while (cursor.moveToNext()) {
            Message person = new Message();
            person.setId(cursor.getInt(cursor.getColumnIndex("_id")));
            person.setTitle(cursor.getString(cursor.getColumnIndex("title")));
            person.setDescription(cursor.getString(cursor.getColumnIndex("description")));
            list.add(person);
        }
        db.close();
        return list;
    }

    //用户注册 存入本地数据库
    public boolean InsertUser(UserInfo userInfo) {
        SQLiteDatabase db = helper.getWritableDatabase();
        String sql = "insert into " + MyHelper.USER_INFO_TABLE
                + "(phone,password) values ("
                + "'" + userInfo.getPhone()
                + "' ," + "'" + userInfo.getPassword() + "'" + ")";
        try {
            db.execSQL(sql);
            return true;
        } catch (SQLException e) {
            Log.e("err", "insert failed");
            return false;
        } finally {
            db.close();
        }

    }

    //根据手机号查询密码
    public UserInfo queryUserByPhone(String phone) {
        SQLiteDatabase db = helper.getReadableDatabase();
        UserInfo userInfo = null;
//		Cursor cursor = db.query(MyHelper.DOCTOR_NAME, null, null,null, null, null, null);
        Cursor cursor = db.rawQuery("select * from userInfo where phone=?", new String[]{phone});
        while (cursor.moveToNext()) {
            userInfo = new UserInfo();
            userInfo.setId(cursor.getInt(cursor.getColumnIndex("_id")));
            userInfo.setPhone(cursor.getString(cursor.getColumnIndex("phone")));
            userInfo.setPassword(cursor.getString(cursor.getColumnIndex("password")));
            return userInfo;

        }
        db.close();
        return userInfo;

    }


    //插入心情日记
    public boolean insertDiary(DiaryInfo diaryInfo) {
        SQLiteDatabase db = helper.getWritableDatabase();
        String sql = "insert into " + MyHelper.DIARY_TABLE
                + "(description) values ("
                + "'" + diaryInfo.getDescription() + "'" + ")";
        try {
            db.execSQL(sql);
            return true;
        } catch (SQLException e) {
            Log.e("err", "insert failed");
            return false;
        } finally {
            db.close();
        }

    }

    //获取心情日记
    public List<DiaryInfo> queryAll1Diary() {
        SQLiteDatabase db = helper.getReadableDatabase();
        List<DiaryInfo> list = new ArrayList<DiaryInfo>();
        Cursor cursor = db.query(MyHelper.DIARY_TABLE, null, null, null, null, null, null);

        while (cursor.moveToNext()) {
            DiaryInfo diaryInfo = new DiaryInfo();
            diaryInfo.setDescription(cursor.getString(cursor.getColumnIndex("description")));
            list.add(diaryInfo);
        }
        db.close();
        return list;
    }

}
